package org.micropayment.consumer.oauth.service;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.micropayment.model.ProviderResource;
import org.springframework.security.oauth.common.signature.RSAKeySecret;
import org.springframework.security.oauth.common.signature.SharedConsumerSecret;
import org.springframework.security.oauth.common.signature.SignatureSecret;
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 * @author jj
 */
public class MicropaymentConsumerResourceDetails implements ProtectedResourceDetails {

    private HttpServletRequest request;
    private Map<String, String> additionalParameters;
    private Map<String, String> additionalRequestHeaders;
    private ProviderResource providerResource;

    public MicropaymentConsumerResourceDetails (ProviderResource providerResource) {
        this.providerResource = providerResource;
        request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
    }

    public String getId() {
        return providerResource.getId();
    }

    public String getConsumerKey() {
        return providerResource.getConsumerKey();
    }

    public String getSignatureMethod() {
        return providerResource.getSignatureMethod();
    }

    public SignatureSecret getSharedSecret() {
        SignatureSecret signatureSecret = null;
        switch (providerResource.getTypeofsecret()) {
            case RSAKeySecret:
                signatureSecret = new RSAKeySecret(providerResource.getConsumerKey());
                break;
            case SharedConsumerSecret:
                signatureSecret = new SharedConsumerSecret(
                        providerResource.getSharedConsumerSecret());
                break;
        }
        return signatureSecret;
    }

    public String getRequestTokenURL() {
        return providerResource.getRequestTokenURL();
    }

    public String getRequestTokenHttpMethod() {
        return providerResource.getRequestTokenHttpMethod();
    }

    public String getUserAuthorizationURL() {
        return providerResource.getUserAuthorizationURL();
    }

    public String getAccessTokenURL() {
        return providerResource.getAccessTokenURL();
    }

    public String getAccessTokenHttpMethod() {
        return providerResource.getAccessTokenHttpMethod();
    }

    public boolean isAcceptsAuthorizationHeader() {
        return providerResource.getAcceptsAuthorizationHeader();
    }

    public String getAuthorizationHeaderRealm() {
        return providerResource.getAuthorizationHeaderRealm();
    }

    public boolean isUse10a() {
        return providerResource.getUse10a();
    }

    public Map<String, String> getAdditionalParameters() {
        additionalParameters = new HashMap<String, String>();
        additionalParameters.put("oauth_transaction_amount",
                request.getParameter("transaction_amount"));
        additionalParameters.put("oauth_transaction_description",
                request.getParameter("transaction_description"));
        return additionalParameters;
    }

    public Map<String, String> getAdditionalRequestHeaders() {
        return additionalRequestHeaders;
    }

}
